package com.lyc.mapper;

import com.lyc.pojo.Like;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;

import java.util.List;

@Mapper
public interface LkMapper {

    // 获取用户对某个帖子的所有互动（点赞和收藏）
    @Select("SELECT * FROM likes WHERE user_id = #{userId} and post_id = #{postId}")
    List<Like> getLkId(Integer userId, Integer postId);

    // 添加点赞
    @Insert("INSERT INTO likes (user_id, post_id, type, created_at) " +
            "VALUES " +
            "(#{userId}, #{postId}, #{type}, NOW())")
    void addLike(Integer userId, Integer postId, String type);

    // 添加收藏
    @Insert("INSERT INTO likes (user_id, post_id, type) " +
            "VALUES" +
            " (#{userId}, #{postId}, #{type})")
    void addFavorite(Integer userId, Integer postId, String type);

    // 获取用户点赞，收藏列表
    @Select("SELECT * FROM likes WHERE user_id = #{userId} and type = #{type}")
    List<Like> getUserLkList(Integer userId,String type);

    // 判断当前用户是否点赞
    @Select("SELECT * FROM likes WHERE user_id = #{userId} and post_id = #{postId} and type = 'like'")
    Like isLiked(Integer userId, Integer postId);

    // 获取当前用户是否收藏
    @Select("SELECT * FROM likes WHERE user_id = #{userId} and post_id = #{postId} and type = 'favorite'")
    Like isFavorite(Integer userId, Integer postId);

    // 获取作者
    @Select("SELECT nickname FROM users WHERE id = #{userId}")
    String getAuthor(Integer userId);

    // 获取点赞数
    @Select("SELECT COUNT(*) FROM likes WHERE post_id = #{postId} and type = 'like' ")
    Integer getLikeCount(Integer postId);

    // 获取帖子收藏总数
    @Select("SELECT COUNT(*) FROM likes WHERE post_id = #{postId} and type = 'favorite' ")
    Integer getFavoriteCount(Integer postId);

    // 取消点赞，收藏
    @Delete("DELETE FROM likes WHERE id = #{lkId}")
    void removeLkList(Integer lkId);

}